import random as rand
import math
import docx
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.enum.text import WD_BREAK

oper = ['+', '-', "*", "/"]
colName = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
doc = docx.Document()

'''初级加减混合'''
def saveToDocx(dataList: list, colNum: int = 3, timeDelta: int = 0):
    dataLen = len(dataList)
    rowNum = math.ceil(dataLen/colNum)

    ph = doc.add_paragraph('  ')
    if timeDelta > 0:
        run = ph.runs[0]
        run.add_break(WD_BREAK.PAGE)
    idx = 0
    font = ph.runs[0].font
    font.size = docx.shared.Pt(16)
    table = doc.add_table(rows=rowNum, cols=colNum)
    for i in range(0, rowNum):
        for j in range(0, colNum):
            cellTmp = table.cell(i, j)
            cellTmp.text = dataList[idx]
            cellTmp.paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
            cellTmp.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER
            cellTmp.vertical_alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
            cellTmp.paragraphs[0].runs[0].font.bold = True
            font = cellTmp.paragraphs[0].runs[0].font
            font.size = docx.shared.Pt(16)
            idx += 1
            if idx == dataLen:
                break


'''生成算式'''


def gen_func(num: [], bracketPos: int, oper=''):
    func_str = ''
    numPos = -1
    for i, n in enumerate(num):
        if type(n) is int:
            numPos = numPos+1
        if numPos == bracketPos:
            numPos = numPos + 1
            func_str = func_str + '(     )'
            continue
        func_str = func_str + str(n)
    return func_str


''' 小学生加减乘除基础运算 '''
if __name__ == '__main__':

    for p in range(0, 14):
        express = []
        numarr = []
        for i in range(1, 31):
            bracketPos = rand.randint(0, 3)
            opCode = oper[rand.randint(0, 3)]

            if opCode == '*' or opCode == '/':
                num1 = rand.randint(2, 9)
                num2 = rand.randint(2, 9)
                num3 = num1 * num2
                if opCode == "/":
                    numarr.append(num3)
                    numarr.append(opCode)
                    numarr.append(num1)
                    numarr.append("=")
                    numarr.append(num2)
                else:
                    numarr.append(num1)
                    numarr.append(opCode)
                    numarr.append(num2)
                    numarr.append("=")
                    numarr.append(num3)
                bracketPos = rand.randint(0, 1)
            else:
                numarr.append(rand.randint(1, 50))
                numarr.append(oper[rand.randint(0, 1)])
                numarr.append(rand.randint(1, 50))
                numarr.append(oper[rand.randint(0, 1)])
                numarr.append(rand.randint(1, 50))
                numarr.append("=")
                numarr.append(rand.randint(1, 50))
            express.append(gen_func(numarr, bracketPos, opCode))
            numarr = []
        saveToDocx(express, 2, p)
    doc.save('数学题.docx')
